import Vue from "vue";
import "./plugins/axios";
import App from "./App.vue";
import router from "./router";
import "./assets/iconfont/iconfont.css";
import "@vant/touch-emulator";

import { PullRefresh, Lazyload, Popup, Tab, Tabs, Slider, Icon } from "vant";
Vue.use(Icon);
Vue.use(Lazyload);
Vue.use(PullRefresh);
Vue.use(Popup);
Vue.use(Tab).use(Tabs);
Vue.use(Slider); //Slider滑块
// import './assets/minireset.min.css'
// import './assets/animate.min.css'
Vue.config.productionTip = false;
//把播放音乐相关的数据存储在vue实例中
//在整个项目组件里面都可以通过this.$root.musicStore拿到这数据
let musicStore = {
    musicID: null,
    musicList: [], //当前播放列表
    currentMusic: null, //记录当前播放的音乐的信息
    picUrl: null,
    text: null,
};
new Vue({
    data: {
        musicStore,
        lyric: [],
        islyric: true,
    },
    watch: {
        musicStore: {
            handler() {
                //侦听执行函数
                let musicID = this.musicStore.musicID;
                //根据musicID获取歌词
                // this.$http.get('http://music.kele8.cn/lyric?id=' + musicID)
                if (musicID) {
                    this.$http
                        .get("/lyric?id=" + musicID)
                        .then((data) => {
                            this.$root.islyric = true;
                            // console.log(data);
                            // this.lyric = data.data.lrc;
                            let lyricArr = data.data.lrc.lyric.split("\n");
                            lyricArr.pop(); //取出匹配不到最后一条
                            let reg = /\[(\d+):(\d+\.\d+)\](.*)/;
                            let newLiric = lyricArr.map((d) => {
                                if (reg.test(d)) {
                                    return {
                                        time: parseInt(RegExp.$1) * 60 + parseFloat(RegExp.$2),
                                        lyric: RegExp.$3,
                                    };
                                }
                            });

                            this.lyric = newLiric;
                        })
                        .catch((err) => {
                            console.log(err);
                            this.lyric = {};
                            this.$root.islyric = false;
                        });
                }
            },
            deep: true, //侦听对象里面所有的变化
        },
    },
    router,
    render: (h) => h(App),
}).$mount("#app");